﻿using Dapper;
using System.Data.SqlClient;
using WebApplication1.Entity;
using yunbo.Model.BIP;

namespace yunbo.Repositorys.FPMRepositorys
{
    public class Mbd_BOMxqRepository
    {
        public string data = "Data Source=121.37.93.71;Initial Catalog=FanXingMES;User ID=sa;Password=geili@2025;";

        public List<Mbd_BOMxgNode> GetBomTree(string bomcode)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"DECLARE @currentBomCode VARCHAR(255);  SET @currentBomCode = '{bomcode}'; WITH BomCTE AS ( SELECT * FROM mbd_bomxq WHERE bom_code = @currentBomCode UNION ALL SELECT b.* FROM mbd_bomxq b INNER JOIN BomCTE c ON b.bomxq_pcode = c.bomxq_code ) SELECT * FROM BomCTE;  ";
                List<Mbd_BOMxg> boms = connection.Query<Mbd_BOMxg>(sql).ToList(); // 使用 ToList() 获取所有BOM详情  
                return GetBomTreeData(boms, boms[0].bomxq_pcode); // 获取根节点，假设根节点的 pcode 为 null  
            }
        }

        public List<Mbd_BOMxgNode> GetBomTreeData(List<Mbd_BOMxg> boms, string parentPcode)
        {
            List<Mbd_BOMxgNode> bomNodes = new List<Mbd_BOMxgNode>();

            // 从BOM列表中筛选出父级 PCode 匹配的 BOM 项  
            var childBoms = boms.Where(b => b.bomxq_pcode == parentPcode).ToList();

            foreach (var bom in childBoms)
            {
                Mbd_BOMxgNode node = new Mbd_BOMxgNode
                {
                    bomxq_id = bom.bomxq_id,
                    bomxq_code = bom.bomxq_code,
                    wl_code = bom.wl_code,
                    wl_name = bom.wl_name,
                    bom_code = bom.bom_code,
                    wl_xh = bom.wl_xh,
                    wl_fl = bom.wl_fl,
                    wl_lx = bom.wl_lx,
                    dw_code = bom.dw_code,
                    dw_name = bom.dw_name,
                    bomxq_pcode = bom.bomxq_pcode,
                    bomxq_sl = bom.bomxq_sl,
                    is_deleted = bom.is_deleted,
                    create_by = bom.create_by,
                    create_time = bom.create_time,
                    update_by = bom.update_by,
                    update_time = bom.update_time,
                    Children = GetBomTreeData(boms, bom.bomxq_code) // 递归获取子节点  
                };
                bomNodes.Add(node);
            }

            return bomNodes;
        }

        public List<Mbd_BOMxg> GetMbd_BOMxg(string bomcode)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"DECLARE @currentBomCode VARCHAR(255);  SET @currentBomCode = '{bomcode}'; WITH BomCTE AS ( SELECT * FROM mbd_bomxq WHERE bom_code = @currentBomCode UNION ALL SELECT b.* FROM mbd_bomxq b INNER JOIN BomCTE c ON b.bomxq_pcode = c.bomxq_code ) SELECT DISTINCT *  \r\nFROM BomCTE  \r\nWHERE bom_code IS NOT NULL;   ";
                List<Mbd_BOMxg> boms = connection.Query<Mbd_BOMxg>(sql).ToList(); // 使用 ToList() 获取所有BOM详情  
                return boms;// 获取根节点，假设根节点的 pcode 为 null  
            }
        }
        public List<Mbd_BOMxg> GetBom(string wl_code)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"select * from mbd_bomxq where bomxq_pcode=(select bomxq_code from mbd_bomxq where wl_code='{wl_code}') ";
                List<Mbd_BOMxg> boms = connection.Query<Mbd_BOMxg>(sql).ToList(); // 使用 ToList() 获取所有BOM详情  
                return boms;
            }
        }
    }
}
